/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:58 AM
------------------------------------------------------*/
using hou_elearning.DataAccess.Base;
using hou_elearning.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace hou_elearning.DataAccess
{    
    public class BaihocDAL : SqlProvider<BaihocEntity>
    {
        static BaihocDAL()
        {
            InitReader();
        }
        protected static void InitReader()
        {
            getFromReader=delegate(SqlDataReader dr)
            {
                BaihocEntity entity = new BaihocEntity();
				entity.PK_iBaihocID = Int64.Parse("0"+dr["PK_iBaihocID"].ToString());
				entity.sTenbaihoc = dr["sTenbaihoc"].ToString();
				entity.sThongtinbaihoc = dr["sThongtinbaihoc"].ToString();
				entity.iTrangthai = Int16.Parse("0"+dr["iTrangthai"].ToString());
				entity.dNgaytao =String.IsNullOrEmpty(dr["dNgaytao"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaytao"].ToString());
				entity.dNgaycapnhat =String.IsNullOrEmpty(dr["dNgaycapnhat"].ToString())?DateTime.Now:DateTime.Parse(dr["dNgaycapnhat"].ToString());
                entity.FK_iAnhminhhoaID = Int64.Parse("0" + dr["FK_iAnhminhhoaID"].ToString());
				entity.FK_iNguoitaoID = Int64.Parse("0"+dr["FK_iNguoitaoID"].ToString());
				entity.FK_iNguoicapnhat = Int64.Parse("0"+dr["FK_iNguoicapnhat"].ToString());
                return entity;
            };
        }
        public static BaihocEntity GetOne(Int64 PK_iBaihocID)
        {
            string cmdName = "spBaihoc_GetByPK";
            SqlParameter p = new SqlParameter("@PK_iBaihocID", PK_iBaihocID);
            BaihocEntity entity = GetOne(cmdName, p);
            return entity;
        }
        public static List<BaihocEntity> GetAll()
        {
            string cmdName = "spBaihoc_Get";
            return GetList(cmdName);
        }
        public static List<BaihocEntity> GetTop()
        {
            string cmdName = "spBaihoc_GetTop";
            return GetList(cmdName);
        }
        public static List<BaihocEntity> GetTopByTime()
        {
            string cmdName = "spBaihoc_GetTopByTime";
            return GetList(cmdName);
        }
        public static List<BaihocEntity> GetByFK_iNguoitaoID(Int64 FK_iNguoitaoID)
		{
			string cmdName = "spBaihoc_GetByFK_FK_iNguoitaoID";
			SqlParameter p = new SqlParameter("@FK_iNguoitaoID",FK_iNguoitaoID);
			List<BaihocEntity> list = GetList(cmdName, p);
			return list;
		}public static List<BaihocEntity> GetByFK_iNguoicapnhat(Int64 FK_iNguoicapnhat)
		{
			string cmdName = "spBaihoc_GetByFK_FK_iNguoicapnhat";
			SqlParameter p = new SqlParameter("@FK_iNguoicapnhat",FK_iNguoicapnhat);
			List<BaihocEntity> list = GetList(cmdName, p);
			return list;
		}
        public static int Add(BaihocEntity entity)
        {
            string cmdName = "spBaihoc_Insert";
            return Run(cmdName,true,initParams(entity));

        }
        public static bool Edit(BaihocEntity entity)
        {
            string cmdName = "spBaihoc_Update";
            return Run(cmdName,false,initParams(entity))>0;
        }
        public static bool Remove(Int64 PK_iBaihocID)
        {
            string cmdName = "spBaihoc_Delete";
            SqlParameter p = new SqlParameter("@PK_iBaihocID", PK_iBaihocID);
            return Run(cmdName,false,p)>0;
        }
        #region private
        private static SqlParameter[] initParams(BaihocEntity entity)
        {
            SqlParameter[] p = new SqlParameter[9];
			p[0] = new SqlParameter("@PK_iBaihocID", entity.PK_iBaihocID);
			p[1] = new SqlParameter("@sTenbaihoc", entity.sTenbaihoc);
			p[2] = new SqlParameter("@sThongtinbaihoc", entity.sThongtinbaihoc);
			p[3] = new SqlParameter("@iTrangthai", entity.iTrangthai);
			p[4] = new SqlParameter("@dNgaytao", entity.dNgaytao);
			p[5] = new SqlParameter("@dNgaycapnhat", entity.dNgaycapnhat);
            p[6] = new SqlParameter("@FK_iAnhminhhoaID", entity.FK_iAnhminhhoaID);
			p[7] = new SqlParameter("@FK_iNguoitaoID", entity.FK_iNguoitaoID);
			p[8] = new SqlParameter("@FK_iNguoicapnhat", entity.FK_iNguoicapnhat);
            return p;
        }
        #endregion
       
    }
}